//
//  WNSearchView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/4.
//

import UIKit

class WNSearchView: UIView {
    
    lazy var searchTF: WNTextField = {
        let tf = WNTextField()
        tf.font = .wn_font(name: .regular, size: 12)
        tf.backgroundColor = .init(named: "white_F7F7F7")
        tf.keyboardType = .default
        tf.returnKeyType = .search
        tf.rx.controlEvent([.editingChanged]).asObservable()
            .subscribe(onNext: { [weak self] in
                tf.text = String(tf.text?.prefix(100) ?? "")
            }).disposed(by: rx.disposeBag)
        tf.rx.controlEvent([.editingDidEndOnExit]).asObservable()
            .subscribe(onNext: { [weak self] in
                guard tf.text?.trimmingCharacters(in: .whitespacesAndNewlines).count != 0 else {
                    return
                }
                self?.searchClosure?(tf.text ?? "")
            }).disposed(by: rx.disposeBag)
        tf.placeholderStr = "搜尋故事/作家/標籤"
        let leftView = UIView.init(frame: CGRect(x: 0, y: 0, width: 36, height: 32))
        let leftImage = UIImageView(image: UIImage(named: "library_search"))
        leftImage.frame = CGRect(x: 10, y: 4, width: 24, height: 24)
        leftView.addSubview(leftImage)
        tf.leftView = leftView
        tf.wn_cornerWithBorder(4)
        return tf
    }()
        
    lazy var cancelBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("取消", for: .normal)
        btn.setTitleColor(.black, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 12)
        btn.titleLabel?.textAlignment = .center
        btn.rx.tap.bind { [weak self] in
            self?.btnClosure?()
        }.disposed(by: rx.disposeBag)
        btn.isHidden = true
        return btn
    }()

    lazy var taskBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setImage(UIImage(named: "discover_task"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.viewContainingController?.wn_pushTaskVC()
        }.disposed(by: rx.disposeBag)
        btn.isHidden = true
        return btn
    }()
        
    lazy var rankBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setImage(UIImage(named: "discover_rank"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.viewContainingController?.wn_pushEarnVC()
        }.disposed(by: rx.disposeBag)
        btn.isHidden = true
        return btn
    }()
    
    lazy var readImage: UIImageView = {
        let view = UIImageView()
        view.backgroundColor = .init(named: "red_FE6390")
        view.wn_cornerWithBorder(4)
        view.isHidden = true
        return view
    }()

    lazy var searchBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.rx.tap.bind { [weak self] in
            self?.viewContainingController?.wn_presentSearchVC()
        }.disposed(by: rx.disposeBag)
        btn.isHidden = true
        return btn
    }()

        
    var searchClosure: StringClosure?
    
    var btnClosure: ReturnClosure?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(cancelBtn)
        addSubview(taskBtn)
        addSubview(rankBtn)
        rankBtn.addSubview(readImage)
        addSubview(searchTF)
        addSubview(searchBtn)
        cancelBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(-12)
            make.size.equalTo(CGSize(width: 50, height: 32))
        }
        taskBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(-12)
            make.width.height.equalTo(24)
        }
        rankBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(taskBtn.snp.left).offset(-12)
            make.width.height.equalTo(24)
        }
        readImage.snp.makeConstraints { (make) in
            make.top.equalTo(-4)
            make.right.equalTo(6)
            make.width.height.equalTo(8)
        }
        searchTF.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(12)
            make.right.equalTo(cancelBtn.snp.left).offset(-12)
            make.height.equalTo(32)
        }
        searchBtn.snp.makeConstraints { (make) in
            make.edges.equalTo(searchTF.snp.edges)
        }
    }
    
    /*
     0: 发现页, 2: 搜索页
     */
    func searchType(_ type: Int) {
        switch type {
        case 0:
            searchBtn.isHidden = false
            taskBtn.isHidden = false
            rankBtn.isHidden = false
            readImage.isHidden = false
            searchTF.snp.updateConstraints { make in
                make.right.equalTo(cancelBtn.snp.left).offset(-24)
            }
        case 2:
            cancelBtn.isHidden = false
        default: break
        }
        layoutIfNeeded()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
